//
//  HMHtmlViewController.m
//  01-网易彩票
//
//  Created by SZSYKT_iOSBasic_2 on 16/2/22.
//  Copyright © 2016年 heima. All rights reserved.
//

#import "HMHtmlViewController.h"
#import "HMHelp.h"

@interface HMHtmlViewController() <UIWebViewDelegate>

@end

@implementation HMHtmlViewController

- (void)loadView {
    self.view = [[UIWebView alloc] init];
}

//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//       NSLog(@"UIWebView = %@",self.view);
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // JS: Javascript 脚本语言  解析型语言  解析一行执行一行
    /*
     var age = 100;
     var name = "zhangsan"
     xxxxx000
     */
    // OC: Objective C 面向对象 编译型语言 所有的代码必须全部编译通过才能执行
    /*
     int age = 100;
     NSString *name = @"jack";
     xxxxx
     */
 
//    UIWebView 显示网页文件html，ppt,视频，word,pdf...
    // 设置标题
    self.title = self.help.title;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    self.view.backgroundColor = [UIColor redColor];
    
    UIWebView *webView = (UIWebView *)self.view;
    // 设置代理
    webView.delegate = self;
    
    // 创建url对象
    // NSURL:格式  协议://主机地址/路径  http:ftp
    // @"http://www.baidu.com"
//    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    
    // 获得文件全路径
    NSString *filePath = [[NSBundle mainBundle] pathForResource:self.help.html ofType:nil];
    NSURL *url = [NSURL fileURLWithPath:filePath]; // 返回的url是带协议头的
//    NSLog(@"url = %@",url);
    // 创建请求对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 加载请求对象对应的资源文件
    [webView loadRequest:request];
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIWebViewDelegate 代理方法
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //    window.location.href = "#howtorecharge"
    if (!self.help.Id) return;
    NSLog(@"%s", __FUNCTION__);
    // 获得js代码
    NSString *js = [NSString stringWithFormat:@"window.location.href = \"#%@\"",self.help.Id];
    // 执行js代码
    [webView stringByEvaluatingJavaScriptFromString:js];
    
}

@end
